//
//  LWTabBar.swift
//  MicroBlog
//
//  Created by 孙程 on 2017/7/14.
//  Copyright © 2017年 孙程. All rights reserved.
//

import UIKit

//自定义tabbar
class LWTabBar: UITabBar {
    //点击撰写按钮执行的 闭包
    var composeBtnClouser:(()->())?
    
    ///懒加载
    private lazy var composeBtn:UIButton = {
        let button = UIButton()
        //添加点击事件
        button .addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
        //图片
        button .setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        button .setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        //背景图片
        button .setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        
        //设置大小
        button.sizeToFit()
        
        return button
        
    }()
    
    //重写inint方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加控件的函数
    private func setupUI(){
        //添加自视图控件
        addSubview(composeBtn)
        
    }
    //Mark - 点击事件
    func composeBtnClick(){
        print("9999")
        composeBtnClouser?()
    }
    //设置子控件的位置
    override func layoutSubviews() {
        super.layoutSubviews()
        composeBtn.centerX = width * 0.5
        composeBtn.centerY = height * 0.5
        
        //计算每项按钮的宽度
        let itemWidth = width / 5
        
        //记录是遍历到的第几个
        var index = 0
        
        //获取系统按钮的控件
        for value in subviews{
            if value .isKind(of:NSClassFromString("UITabBarButton")!) {
                value.width = itemWidth
                
                //设置系统按钮的X坐标
                value.x = CGFloat(index) * itemWidth
                
                index += 1
                
                if index == 2
                {
                    index += 1
                }
                
            }
        }
        
        
    }
    
    

}
